#include "PLCHelper.h"
#include <QDebug>


double gMotorposs;
double gMotorangle1s;
double gMotorangle2s;

PLCHelper::PLCHelper():
    _mb_ptr{Singleton<mbControl>::get()}
{
    // Singleton<mbControl> _mb_ptr;
}

MU_RETURNS PLCHelper::OpenAirValve()
{
    try {
        _mb_ptr->openAirValve();
    } catch (...) {
        qWarning()<<"PLC伸出传感器异常!"<<__FUNCTION__;
        return MU_RETURN_ERROR;
    }
    return MU_RETURN_SUCCESS;
}

MU_RETURNS PLCHelper::CloseAirValve()
{
    try {
        _mb_ptr->closeAirValve();
    } catch (...) {
        qWarning()<<"PLC收传感器异常!"<<__FUNCTION__;
        return MU_RETURN_ERROR;
    }
    return MU_RETURN_SUCCESS;
}


void PLCHelper::Connect()
{
    if(currentStatus.GetInit(MU_DEVICE_PLC)){     /// 如果已经初始化了
        qWarning()<<"PLC已经初始化成功!"<<__FUNCTION__;
        return ;
    }
    currentStatus.setInitType(MU_DEVICE_PLC,MU_TY_RUNNING);
    std::future<MU_RETURNS> result = std::async(std::launch::async,&PLCHelper::_connect,this);


    if(MU_RETURN_SUCCESS != result.get()){
        currentStatus.initFailed(MU_DEVICE_PLC);
        std::cout << "PLC连接:" << MU_Get_Msg(result.get()) << ".\n";
        return ;
    }

    currentStatus.initSucess(MU_DEVICE_PLC);
}

MU_RETURNS PLCHelper::_connect()
{
    return MU_RETURN_SUCCESS;
}
